﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Entity;
using NLBS.Framework;
using System.Globalization;

namespace NLBS.Data
{
    public static class Cacher
    {

        #region Setting
        private static readonly string SettingKey = "settings";

        public static Dictionary<string, Setting> GetSetting(CacheDuration cacheDuration)
        {
            string key = string.Format(CultureInfo.InvariantCulture, SettingKey);
            ContentCache cache = ContentCache.Instantiate();
            Dictionary<string, Setting> settings = (Dictionary<string, Setting>)cache[key];
            if (settings == null)
            {
                settings = DbProvider.Instance().GetSettings();
                if (settings != null)
                {
                    cache.Insert(key, settings, cacheDuration);
                }
            }
            return settings;
        }

        #endregion


        public static readonly string PreviousAndNextArticleLinkKey = "PreviousAndNextArticleLinkKey:articleID{0}";

        public static IList<ArticleLink> GetPreviousAndNextArticleLink(int articleID)
        {
            string key = string.Format(CultureInfo.InvariantCulture, PreviousAndNextArticleLinkKey, articleID);
            ContentCache cache = ContentCache.Instantiate();
            IList<ArticleLink> lists = (List<ArticleLink>)cache[key];
            if (lists == null)
            {
                lists = DbProvider.Instance().GetPreviousAndNextArticle(articleID);
                if (lists != null)
                {
                    cache.Insert(key, lists, CacheDuration.Long);
                }
            }
            return lists;
        }
        #region Article
        public static readonly string ArticleKey = "ArticleDateKey";
        public static IList<string> GetArticleKey()
        {
            ContentCache cache = ContentCache.Instantiate();
            IList<string> lists = (List<string>)cache[ArticleKey];
            if (lists == null)
            {
                lists = new List<string>();
                if (lists != null)
                {
                    cache.Insert(ArticleKey, lists, CacheDuration.Long);
                }
            }
            return lists;
        }

        public static readonly string RssArticlePageKey = "RssArticlePageKey";

        public static IPagedCollection<Article> GetRssArticle(int pageSize, CacheDuration cacheDuration)
        {
            string key = string.Format(RssArticlePageKey);
            ContentCache cache = ContentCache.Instantiate();
            IPagedCollection<Article> lists = (PagedCollection<Article>)cache[key];
            if (lists == null)
            {
                lists = DbProvider.Instance().GetRssArticleByPage(pageSize);
                if (lists != null)
                {
                    cache.Insert(key, lists, cacheDuration);
                    Cacher.GetArticleKey().Add(key);
                }
            }
            return lists;
        }

        public static readonly string ArticleSelectedPageKey = "ArticleSelectedPage:Index{0}PageSize{1}";

        public static IPagedCollection<Article> GetPageArticleBySelected(int startIndex, int pageSize)
        {
            string key = string.Format(ArticleSelectedPageKey, startIndex, pageSize);
            ContentCache cache = ContentCache.Instantiate();
            IPagedCollection<Article> lists = (PagedCollection<Article>)cache[key];
            if (lists == null)
            {
                lists = DbProvider.Instance().GetSelectedArticleByPage(startIndex, pageSize);
                if (lists != null)
                {
                    cache.Insert(key, lists, CacheDuration.Long);
                    Cacher.GetArticleKey().Add(key);
                }
            }
            return lists;
        }

        public static void RemoveArticleKey()
        {
            ContentCache cache = ContentCache.Instantiate();
            for (int i = 0; i < Cacher.GetArticleKey().Count; i++)
            {
                cache.Remove(Cacher.GetArticleKey()[i]);
            }
        }

        #endregion

        

        #region Archive

        public static readonly string ArchiveKey = "ArchiveList";

        public static IList<Archive> GetArticleArchive()
        {
            string key = ArchiveKey;
            ContentCache cache = ContentCache.Instantiate();
            IList<Archive> lists = (List<Archive>)cache[key];
            if (lists == null)
            {
                lists = DbProvider.Instance().GetArticleMonthArchive();
                if (lists != null)
                {
                    cache.Insert(key, lists, CacheDuration.Long);
                }
            }
            return lists;
        }
        #endregion
    }

    
}
